package com.max.service;

import com.max.base.BaseServiceImpl;
import com.max.dao.UserinfoDao;
import com.max.entity.Userinfo;
import com.max.utils.CommonUtils;

import java.util.List;

public class UserinfoService extends BaseServiceImpl<UserinfoDao, Userinfo> {

    private final static UserinfoService INSTANCE = new UserinfoService();

    public static UserinfoService getInstance() {
        return INSTANCE;
    }

    private UserinfoService() {
        super(UserinfoDao.getInstance());
    }

    public int register(String username, String loginName, String pwd) {
        if (!CommonUtils.isNotBlank(loginName)) {
            throw new RuntimeException("用户名不能为空");
        }
        if (!CommonUtils.isNotBlank(pwd)) {
            throw new RuntimeException("密码不能为空");
        }
        Userinfo insert = new Userinfo();
        insert.setUsername(username);
        insert.setLoginName(loginName);
        insert.setPwd(pwd);
        return super.insert(insert);
    }


    public Userinfo login(String loginName, String pwd) {
        List<Userinfo> selectList = super.dao.loginSelectList(loginName, pwd);
        if (selectList == null || selectList.isEmpty()) {
            throw new RuntimeException("用户名或密码错误");
        }
        return selectList.get(0);
    }


}
